
"""
A WHIFF resource for storing wiki information.

This simplistic implementation stores data too and from a static
marshal file.

The implementation may very easily be modified to store too and
from other storage structures, such as SQL databases.
"""

import marshal
import os

class wikiStorage:
    
    def __init__(self, filepath, value=None):
        self.filepath = filepath
        self.value = value
        
    def localize(self, env):
        "return a connection between an HTTP request and the storage resource"
        # read in and cache the any data from the file for this request
        value = None
        if os.path.exists(self.filepath):
            f = file(self.filepath, "rb")
            value = marshal.load(f)
        # return a connected storage object
        return wikiStorage(self.filepath, value)
    
    def get(self, pathlist):
        return self.value

    def put(self, pathlist, value):
        self.value = value
        f = file(self.filepath, "wb")
        marshal.dump(value, f)
